<?php
/**
 * Description of Dispatcher
 *
 * @author miho
 */
class BluesCoders_Controller_Dispatcher {

    protected $_controllersLoader;
    protected $_basePath;
    /**
     *
     * @var BluesCoders_Context
     */
    protected $_context;

    /**
     *
     * @var BluesCoders_Request_Http_Factory
     */
    protected $_requestFactory;

    public function dispatch() {
        $this->initializeContext();
        
        $router = $this->_context->getBean('router');

        $controllerId = $router->route($this->_requestFactory->getRequest()->getUri());

        $controller = $this->_context->getBean($controllerId);

        return $controller->doAction($this->_requestFactory->getRequest());
    }

    public function setBasePath($value) {
        $this->_basePath = $value;

        set_include_path(
          implode(PATH_SEPARATOR, array(
            $this->_basePath . '/lib',
            get_include_path(),
            )
          )
        );
    }

    public function setRequestFactory($value) {
        $this->_requestFactory = $value;
    }

    public function initializeContext(BluesCoders_Context $parentContext = null) {
        $contextPath =  'config' . DIRECTORY_SEPARATOR . 'dispatcherContext.xml';
        $this->_context = BluesCoders_Context_Builder_Xml::build($contextPath,$this->_basePath, $parentContext);
        return $this;
    }

}
?>
